package org.videopractice.common;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.tbruyelle.rxpermissions2.RxPermissions;

import org.videopractice.dialog.AuthorizedPromptDialog;


public class PermissionsCheckerHelper {

    private final FragmentActivity activity;
    private final HandleBehavior handleBehavior;
    private String[] permissions;
    private String permissionTips    = "授权设置";
    private String permissionDenyTip = "因读取步数信息和步数兑换、提现要求，需要您提供一下权限:设备信息、存储信息、位置信息。";


    private boolean isFirst;


    public PermissionsCheckerHelper(FragmentActivity activity, HandleBehavior handleBehavior) {
        this.activity = activity;
        this.handleBehavior = handleBehavior;
    }

    public PermissionsCheckerHelper setPermissionTips(String permissionTips){
        this.permissionTips = permissionTips;
        return this;
    }
    public PermissionsCheckerHelper setPermissionDenyTip(String permissionDenyTip){
        this.permissionDenyTip = permissionDenyTip;
        return this;
    }

    /**
     * 验证权限：true为没授权,false为授权*/
    private boolean verifyPermission(String permission) {
        return ContextCompat.checkSelfPermission(activity.getApplicationContext(), permission) == PackageManager.PERMISSION_DENIED;
    }

    /**
     * 申请系统授权行为*/
    @SuppressLint("CheckResult")
    private void permissionGrant() {
        new RxPermissions(activity).request(permissions).subscribe((Boolean isSucceed)-> {
            if (isSucceed) {
                handleBehavior.doAfterAuthorizedHandle(true);
            } else {
                if(isFirst) {
                    isFirst = false;
                    showPermissionDenyTip();
                }else{
                    handleBehavior.doAfterAuthorizedHandle(false);
                }
            }
        });
    }

    /**
     * 授权提示*/
    private void showPermissionTip(){
        AuthorizedPromptDialog authorizedPromptDialog = AuthorizedPromptDialog.build(activity)
                .setGoAuthorizedBtnOnClickListener((view)->permissionGrant());
        authorizedPromptDialog.setOnDismissListener(dialog -> handleBehavior.doAfterAuthorizedHandle(false));
        authorizedPromptDialog.show();
    }

    /**
     * 拒绝授权提示*/
    private void showPermissionDenyTip(){
        AuthorizedPromptDialog authorizedPromptDialog = AuthorizedPromptDialog.build(activity)
                .setGoAuthorizedBtnOnClickListener((view)->{
//                    AppHelper.openSimpleSetting(activity);
                    permissionGrant();
                });
        authorizedPromptDialog.setOnCancelListener(dialog -> handleBehavior.doAfterAuthorizedHandle(false));
        authorizedPromptDialog.show();
    }


    public boolean checkPermission(@NonNull String... permissions){
        this.permissions = permissions;
        for (String permission : permissions) {
            if (verifyPermission(permission)) {
//                showPermissionTip();
                permissionGrant();
                return false;
            }
        }
//        handleBehavior.doAfterAuthorizedHandle(true);
        return true;

    }

    public interface HandleBehavior{
        void doAfterAuthorizedHandle(boolean isSucceed);
    }

}
